using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace FUSE
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Player : GameComponent
    {
        //Statistics
        //Max values
        protected double maxSpeed = 0;
        protected double maxFuel = 0;
        protected double maxArmor = 0;
        protected double maxHealth = 0;
        protected String username = "Fry";

        //Current values
        protected double currentScore = 0d;
        protected double stageStartTime = 0;
        protected string currentTimeElapsed="Unknown"; //may need to get this from galaxy
       
        protected double currentComboCount = 0;
        protected double currentStage = 0;

        //Positions
        //Top Left
        private Vector2 currentScorePosition ;
        private Vector2 currentTimeElapsedPosition;
        private Vector2 currentComboPosition;

        //Top Right
        private Vector2 currentStagePosition;

        //Bottom Right
        private Vector2 currentArmorPosition;
        private Vector2 currentHealthPosition;

        //Bottom Left
        private Vector2 currentSpeedPosition;
        private Vector2 currentFuelPosition;

        //Bottom Mid-Right
        private Vector2 usernamePosition;
        //Player's Ship

        //Player's ship
        public Ship ship;

        //GUI attributes
        public const String SCORE_FONT = "scoreFont";
        private SpriteFont scoreFont;

        //Timer
        public Timer timer;
        public DateTime startTime;
        public TimeSpan elapsedTime;

        public Player(Game game)
            : base(game)
        {

            ContentManager content = Game.Content;
            // Load game font
            scoreFont = content.Load<SpriteFont>("scoreFont");
            int leftX = 10;
            int rightX = 500;
            int topY = 5;
            int bottomY = 560;
            //Top Left

            currentScorePosition = new Vector2(leftX, topY);
            currentTimeElapsedPosition = new Vector2(leftX, topY+20);
            currentComboPosition = new Vector2(leftX, topY+40);

            //Top Right
            currentStagePosition = new Vector2(rightX, topY);//(Game.GraphicsDevice.Viewport.Width-10)

            //Bottom Right
            currentArmorPosition = new Vector2(rightX, bottomY-5);
            currentHealthPosition = new Vector2(rightX, (bottomY-20));

            //Bottom Left
            currentSpeedPosition = new Vector2(leftX, (bottomY-5));
            currentFuelPosition = new Vector2(leftX, (bottomY-20));
            
            //Bottom Mid-Right
            usernamePosition = new Vector2(rightX-150, (bottomY-5));

            timer = new Timer();
            startTime = new DateTime();
            ship = new Ship(game);
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            

            base.Initialize();
        }
        /// <summary>
        /// Use to reInitialize players stats for beginning of each stage
        /// </summary>
        /// <param name="gotStage"></param>
        /// <param name="gotStartTime"></param>
        public void reInitialize(int gotStage)
        {


            currentStage = gotStage;
            currentTimeElapsed = getTime(); //should be all 0
            currentScore = 0;
            currentComboCount = 0;

        }
        public void startTimer()
        {

            if (timer != null)
            {
                stopTimer();
                startTime = DateTime.Now;
            }
            timer = new Timer();
            timer.Enabled = true;
            timer.Interval = 100; //1000 milliseconds to second
            timer.Elapsed += new ElapsedEventHandler(Timer_Tick);

            timer.Start();
        }
        public void stopTimer()
        {
            timer.Stop();
            timer.Dispose();
        }
        /// <summary>
        /// Reinitialize ship conditions inside build screen
        /// </summary>
        /// <param name="gotMaxArmor"></param>
        /// <param name="gotMaxHealth"></param>
        /// <param name="gotMaxSpeed"></param>
        /// <param name="gotMaxFuel"></param>
        public void setPlayerConditions(double gotMaxArmor, double gotMaxHealth, double gotMaxSpeed, double gotMaxFuel)
        {
            this.maxArmor = gotMaxArmor;
            this.maxHealth = gotMaxHealth;
            this.maxSpeed = gotMaxSpeed;
            maxFuel = gotMaxFuel;

            //Initialize ship
            ship.setShipConditions(maxArmor, maxHealth, maxSpeed, maxFuel);
        }
        /// <summary>
        /// Draws the player's statistics on gameplay screen
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {

            //Top Left
            spriteBatch.DrawString(scoreFont, "Score: " + currentScore.ToString(), currentScorePosition, Color.LightGoldenrodYellow);
            spriteBatch.DrawString(scoreFont, "Elapsed Time: " + currentTimeElapsed.ToString(), currentTimeElapsedPosition, Color.LightSalmon);
            spriteBatch.DrawString(scoreFont, "Combos: " + currentComboCount.ToString(), currentComboPosition, Color.LightCyan);
            //Top Right
            spriteBatch.DrawString(scoreFont, "Stage: " + currentStage.ToString(), currentStagePosition, Color.WhiteSmoke);
            //Bottom right
            spriteBatch.DrawString(scoreFont, "Armour: " + ship.currentArmor.ToString(), currentArmorPosition, Color.Orange);
            spriteBatch.DrawString(scoreFont, "Health: " + ship.currentHealth.ToString(), currentHealthPosition, Color.PaleVioletRed);
            //Bottom Left
            spriteBatch.DrawString(scoreFont, "Speed: " + ship.currentSpeed.ToString(), currentSpeedPosition, Color.LightGoldenrodYellow);
            spriteBatch.DrawString(scoreFont, "Fuel: " + ship.currentFuel.ToString(), currentFuelPosition, Color.LightCyan);
            //Bottom Mid-Right
            spriteBatch.DrawString(scoreFont, "Cpt. " + username, usernamePosition, Color.CadetBlue);
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
       

            base.Update(gameTime);
        }
        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            if (sender == timer)
            {
                currentTimeElapsed = getTime();
            }
        }
        public string getTime()
        {
            string TimeInString = "";
            elapsedTime = DateTime.Now.Subtract(startTime);

            TimeInString = elapsedTime.ToString().Substring(0, 11); 
            return TimeInString;
        }
    }
}
